package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.domain.DataResult;
import com.dongdongshop.em.DataResultEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.service.TbContentService;
import com.dongdongshop.util.ConstantUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
/*首页*/
@RequestMapping("dongdongshop")
@Controller
public class dongdongshop {

    @Autowired
    private RedisTemplate redisTemplate;
    @Reference
    private TbContentService contentService;


    @RequestMapping("toIndex")
    public String toIndex(){
        return "index";
    }

    @RequestMapping("selectRoundImg")
    @ResponseBody
    public DataResult selectRoundImg(){
        String imgRedis = ConstantUtils.IMG_REDIS;
        String IMG_ROUND = ConstantUtils.IMG_ROUND;
        List<TbContent> list;
        if(redisTemplate.opsForHash().hasKey(imgRedis,IMG_ROUND)){
            list = (List<TbContent>)redisTemplate.opsForHash().get(imgRedis,IMG_ROUND);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(list);
        }
        try {
            list = contentService.selectByCateId();
            redisTemplate.opsForValue().set(IMG_ROUND,list);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @RequestMapping("recommend")
    @ResponseBody
    public DataResult recommend(){
        String IMG_RECOMMEND = ConstantUtils.IMG_RECOMMEND;
        String imgRedis = ConstantUtils.IMG_REDIS;
        List<TbContent> list;
        if(redisTemplate.opsForHash().hasKey(imgRedis,IMG_RECOMMEND)){
            list = (List<TbContent>)redisTemplate.opsForHash().get(imgRedis,IMG_RECOMMEND);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(list);
        }

        try {
            list = contentService.selectRecommend();
            redisTemplate.opsForValue().set(IMG_RECOMMEND,list);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @RequestMapping("favourite")
    @ResponseBody
    public DataResult favourite(){
        String IMG_FAVOURITE = ConstantUtils.IMG_FAVOURITE;
        String imgRedis = ConstantUtils.IMG_REDIS;
        List<TbContent> list;
        if(redisTemplate.opsForHash().hasKey(imgRedis,IMG_FAVOURITE)){
            list = (List<TbContent>)redisTemplate.opsForHash().get(imgRedis,IMG_FAVOURITE);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(list);
        }
        try {
            list = contentService.favourite();
            redisTemplate.opsForValue().set(IMG_FAVOURITE,list);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

}
